﻿using HK.Core.Extend;
using HK.Core.Serializable.Attribute;
using HK.Core.Serializable.Protocol.Attribute;
using HK.Core.Utils;
using HK.Editor.GUIEx.Styles;
using HK.Editor.Serializable.Protocol.Drawer;
using HK.Editor.Utils;
using UnityEditor;
using UnityEngine;

namespace HK.Editor.Serializable.Drawer
{
    /// <summary>
    /// Drawer - 路径浏览
    /// </summary>
    [CustomPropertyDrawer(typeof(PathBrowseAttribute))]
    public class PathBrowseDrawer : GUIPropertyDrawer<PathBrowseAttribute>, IPathBrowseDrawer
    {
        /// <summary>
        /// 浏览模式
        /// </summary>
        public PathBrowseMode Mode => SelfAttr?.Mode ?? PathBrowseMode.File;

#region Size&Rect

        /// <summary>
        /// 宽度 - 浏览按钮
        /// </summary>
        private static readonly float BrowseBtnWidth = BtnWidthMid;

#endregion

#region Content

        private GUIContent _pathContent = null;

        protected GUIContent PathContent
        {
            get
            {
                if (null != _pathContent) return _pathContent;
                if (null != SelfProperty)
                {
                    _pathContent = UtilsGUIEx.CreateGuiContentByPathOfAsset(SelfProperty.stringValue);
                }
                return _pathContent;
            }
        }

        /// <summary>
        /// 初始化Content
        /// </summary>
        /// <param name="iAttr">Attribute</param>
        /// <param name="iProperty">Property</param>
        /// <param name="iLabel">默认标题</param>
        protected override void InitContents(PathBrowseAttribute iAttr, SerializedProperty iProperty, GUIContent iLabel)
        {
            base.InitContents(iAttr, iProperty, iLabel);

            // 路径
            _pathContent = UtilsGUIEx.CreateGuiContentByPathOfAsset(iProperty.stringValue);
        }

#endregion
        
#region Draw

#region Caption
        
        /// <summary>
        /// 绘制标题
        /// </summary>
        /// <param name="iRect">绘制矩形区域</param>
        /// <param name="iEvent">Event</param>
        /// <param name="iCaption">标题</param>
        /// <returns>绘制的矩形区域</returns>
        protected override Rect DrawCaption(Rect iRect, Event iEvent, GUIContent iCaption)
        {
            var displayRect = iRect;
            var titleRect = displayRect;
            titleRect.y -= 1;
            titleRect.xMin += LabelIntent;
            titleRect.xMax -= (HorizontalSpacing + BrowseBtnWidth + HorizontalSpacing);
            titleRect = UtilsGUIEx.DrawLabel(titleRect, iEvent, iCaption, PathContent, 
                GuiExHelper.MiddleLeftLabel, out var oClickableRect, Options);
            
            var browseBtnRect = titleRect;
            browseBtnRect.xMin = titleRect.xMax + HorizontalSpacing;
            browseBtnRect.xMax = displayRect.xMax - HorizontalSpacing;
            browseBtnRect = UtilsGUIEx.DrawBrowseButton(browseBtnRect, iEvent, SelfProperty.stringValue,
                iBrowseDir =>
                {
                    if (UtilsString.Diff(iBrowseDir, SelfProperty.stringValue))
                    {
                        SelfProperty.stringValue = iBrowseDir;
                    }
                }, PathBrowseMode.File == Mode);
            
            // 挂接事件
            if (Clickable)
            {
                HandleMouseEvents(oClickableRect, iEvent, ControlId, 
                    (iControlId) =>
                    {
                        var browsePath = SelfProperty.stringValue;
                        if (!string.IsNullOrEmpty(browsePath))
                        {
                            UtilsGUIEx.PingAsset(browsePath);
                        }
                    });
            }
            
            // 拖拽事件
            if (Draggable)
            {
                HandleDragAndDrop(oClickableRect, iEvent, iAssets =>
                {
                    if(null == iAssets || 0 >= iAssets.Length) return;
                    if (UtilsString.Diff(iAssets[0], SelfProperty.stringValue))
                    {
                        SelfProperty.stringValue = iAssets[0];
                        _pathContent = null;
                    }
                });
            }
            
            return displayRect;
        }

#endregion

#endregion
        
    }
}
